import { OnInit, Component, Input, ViewChild } from '@angular/core';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { ProfessionDTO } from 'src/app/project/model/icm/icm-base/profession/profession.dto';
import { ObjectUtil } from 'src/app/project/utils/object-util';
import { TABLE_PARAMETER } from '../../setting/table-parameter';
import { NzMessageService } from 'ng-zorro-antd';
import { ProfessionService } from 'src/app/project/service/icm/icm-base/profession/profession.service';
import { STColumn } from '@delon/abc';
import { ProfessionDetailComponent } from './profession-detail.component';
import { ClassService } from 'src/app/project/service/icm/icm-base/class/class.service';

@Component({
  selector: 'app-profession-list',
  templateUrl: './profession-list.component.html',
  styles: [],
})
export class ProfessionListComponent implements OnInit {
  constructor(
    private msg: NzMessageService,
    private professionService: ProfessionService,
    private classService: ClassService,
  ) {}

  @ViewChild('professionDetailComponent', { static: false })
  professionDetailComponent: ProfessionDetailComponent;

  /**
   * 分页参数初始化
   */
  queryOptions: QueryOptions = {
    page: 0,
    size: 20,
    sort: 'id,asc',
  };

  /**
   * 查询参数
   */
  @Input()
  queryParams;

  /**
   * 请求数据标识
   */
  loading = false;

  /**
   * 表格数据
   */
  tableData: Array<ProfessionDTO> = [];

  /**
   * 列表参数
   */
  TABLE_PARAMETER = ObjectUtil.deepClone(TABLE_PARAMETER);

  /**
   * 表格的列定义
   */
  stColumns: STColumn[] = [
    {
      title: '序号',
      render: 'number',
      width: '2%',
      className: 'text-center',
    },
    {
      title: '所属学院',
      index: 'academyName',
      width: '4%',
      className: 'text-left',
    },
    {
      title: '专业编码',
      index: 'code',
      width: '4%',
      className: 'text-left',
    },
    {
      title: '专业名称',
      index: 'name',
      width: '4%',
      className: 'text-left',
    },
    {
      title: '专业简称',
      index: 'shortName',
      width: '4%',
      className: 'text-left',
      default: '无',
    },
    {
      title: '显示顺序',
      index: 'showOrder',
      width: '3%',
      className: 'text-center',
      sort: this.TABLE_PARAMETER.sortDef,
    },
    {
      title: '操作',
      render: 'operations',
      index: 'operation',
      width: '4%',
      className: 'text-center',
      fixed: 'right',
      acl: ['ROLE_SENATOR'],
    },
  ];

  /**
   * 表格变化事件，用于双击、排序，翻页等操作
   * @param e event
   */
  change(e: any): void {
    // 双击事件
    if (e.type === 'dblClick') {
      this.loading = true;
      // tslint:disable-next-line: deprecation
      this.professionService.findById(e.dblClick.item.id, false).subscribe(
        data => {
          this.professionDetailComponent.edit(true, data);
        },
        null,
        () => {
          this.loading = false;
        },
      );
    }
    // 排序事件
    if (e.type === 'sort') {
      if (e.sort.map) {
        const temp = e.sort.map.sort;
        if (temp) {
          this.queryOptions.sort = temp.replace(/[\.-]/g, ',');
        }
      }
      this.loadData();
    }
    // 翻页设置
    if (e.type === 'ps' || e.type === 'pi') {
      this.queryOptions.page = e.pi - 1;
      this.queryOptions.size = e.ps;
      this.loadData();
    }
  }

  /**
   * 获取学院数据
   */
  loadData() {
    this.loading = true;
    this.professionService
      .findOnePage(this.queryOptions, this.queryParams.code, this.queryParams.name, this.queryParams.academyId)
      // tslint:disable-next-line: deprecation
      .subscribe(
        data => {
          if (data) {
            this.tableData = data.data;
            this.TABLE_PARAMETER.page.total = data.totalRecords;
          }
        },
        null,
        () => {
          this.loading = false;
        },
      );
  }

  /**
   * 添加专业
   */
  create() {
    this.professionDetailComponent.edit(false, null);
  }

  /**
   * 修改专业信息
   * @param row 专业数据
   */
  onUpdate(row: ProfessionDTO) {
    this.loading = true;
    // tslint:disable-next-line: deprecation
    this.professionService.findById(row.id, false).subscribe(
      data => {
        this.professionDetailComponent.edit(false, data);
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 删除专业
   */
  onDelete(row: ProfessionDTO) {
    // 判断该专业是否存在班级
    this.classService.findOnePage(this.queryOptions, null, null, '', null, row.id).subscribe(data => {
      if (data) {
        if (data.data.length !== 0) {
          this.msg.error('删除失败');
          this.msg.error('该专业尚存在班级');
        } else {
          this.professionService.delete(row.id).subscribe(
            () => {
              this.msg.success('删除成功');
              this.loadData();
            },
            () => this.msg.error('删除失败'),
            () => this.loadData(),
          );
        }
      }
    });
  }

  ngOnInit() {
    this.loadData();
  }
}
